package com.zero.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

    //用final修饰符 固定常量  常量命名 是 全部大写 字母间用下划线
    private static final String ADD_STUDENT = "1";
    private static final String DELETE_STUDENT = "2";
    private static final String UPDATE_STUDENT = "3";
    private static final String QUERY_STUDENT = "4";
    private static final String EXIT = "5";


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        //给循环命名 loop 循环
        loop:
        while (true) {
            System.out.println("-----------------欢迎来到学生管理系统-------------------");
            System.out.println("1: 添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入你的选择：");
            String choose = sc.next();
            switch (choose) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case QUERY_STUDENT -> queryStudent(list);
                case EXIT -> {
                    System.out.println("退出");
                    //跳出指定循环  break loop;
                    System.exit(0);//停止虚拟机运行  两种方法结束程序
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        String inputId = null;
        while (true) {
            System.out.println("请输入学生id");
            inputId = sc.next();
            if (contains(list, inputId)) {
                System.out.println("id已经存在，请重新输入");
            } else {
                s.setId(inputId);
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String inputName = sc.next();
        s.setName(inputName);
        System.out.println("请输入学生年龄");
        int inputAge = sc.nextInt();
        s.setAge(inputAge);
        System.out.println("请输入学生家庭住址");
        String inputAddress = sc.next();
        s.setAddress(inputAddress);
        list.add(s);
        System.out.println("学生信息添加成功");


    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String inputID = sc.next();
        int index = getIndex(list,inputID);
        if (index >= 0) {
            //.remove 删除集合索引上的元素
            list.remove(list.get(index));
            System.out.println("id为"+ inputID + "的学生删除成功");
        }else {
            System.out.println("id不存在，删除失败");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生信息的id");
        String inputID = sc.next();
        int index = getIndex(list,inputID);
        if (index == -1) {
            System.out.println("当前要修改的id不存在，修改失败");
            return;
        }
        Student newStudent = list.get(index);
        System.out.println("请输入要修改的学生姓名");
        String inputName = sc.next();
        newStudent.setName(inputName);
        System.out.println("请输入要修改的学生年龄");
        int inputAge = sc.nextInt();
        newStudent.setAge(inputAge);
        System.out.println("请输入要修改的学生家庭地址");
        String inputAddress = sc.next();
        newStudent.setAddress(inputAddress);
        System.out.println("学生信息修改成功");

    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
        }
    }

    //判断 集合内 学生id 是否唯一
     public static boolean contains(ArrayList<Student> list, String inputId) {
       /* for (int i = 0; i < list.size(); i++) {
            String sid = list.get(i).getId();
            if (sid.equals(inputId)) {
                return true;
            }
        }
        return false;*/

        //两个方法重复 索引下标大于等于0 时肯定存在
       return getIndex(list, inputId) >= 0;
    }

    //删除学生判断  判断学生是否存在  存在返回索引大于等于0  不存在返回-1
    public static int getIndex(ArrayList<Student> list, String inputId) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(inputId)) {
                return i;
            }
        }
        return -1;
    }
}

